package com.ayf.payment.game.api.utils;


import com.ayf.payment.game.api.type.Date2LongFormatEnum;
import com.swwx.charm.commons.lang.utils.LogPortal;
import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


public class DateUtils {

    public static final String formatPattern = "yyyy-MM-dd";

    public static final String formatPattern_Short = "yyyyMMdd";
    public static final String formatPattern_full_01 = "yyyyMMddHHmmss";
    public static final String formatPattern_full_02 = "yyMMddHHmmss";

    public static final String formatPattern_full_03 = "yyMMddHHmm";
    public static final String formatPattern_full_04 = "yyMMdd";


    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static final String formatPattern_full = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取当前日期
     * @return
     */
    public static String getCurrentDate(){
        SimpleDateFormat format = new SimpleDateFormat(formatPattern);
        return format.format(new Date());
    }

    /**
     * 获取当前日期
     * @param formatPattern 时间格式
     * @return
     */
    public static String getCurrentDate(String formatPattern){
        SimpleDateFormat format = new SimpleDateFormat(formatPattern);
        return format.format(new Date());
    }

    /**
     *
     * 获取前几天的日期
     */
    public static String getPrefixDate(int count){
        Calendar cal = Calendar.getInstance();
        int day = 0 - count;
        cal.add(Calendar.DATE,day);   // int amount   代表天数
        Date datNew = cal.getTime();
        return sdf.format(datNew);
    }
    /**
     * 日期转换成字符串
     * @param date
     * @return
     */
    public static String dateToString(Date date,String formatPattern){
        if(StringUtils.isBlank(formatPattern)){
            formatPattern = formatPattern_Short;
        }
        SimpleDateFormat format = new SimpleDateFormat(formatPattern);
        return format.format(date);
    }

    /**
     * 日期转换成字符串
     * @param date
     * @return
     */
    public static String dateToString(Date date){
        if(date==null)
            return  null;
        return sdf.format(date);
    }

    /**
     * 字符串转换日期
     * @param str
     * @return
     */
    public static Date stringToDate(String str){
        //str =  " 2008-07-10 19:20:00 " 格式
        SimpleDateFormat format = new SimpleDateFormat(formatPattern);
        if(StringUtils.isNotEmpty(str)){
            try {
                return format.parse(str);
            } catch (ParseException e) {
                LogPortal.error("字符串[{}]转换[{}]格式的日期，异常信息是:",e,str,formatPattern);
            }
        }
        return null;
    }

    /**
     * 日期字符串格式化
     *
     * @param str
     * @return
     */
    public static String stringFormat(String str, String format1, String format2) {
        SimpleDateFormat format = new SimpleDateFormat(format1);
        if (StringUtils.isNotEmpty(str)) {
            try {
                Date parse = format.parse(str);
                return dateToString(parse, format2);
            } catch (ParseException e) {
                LogPortal.error("字符串[{}]转换[{}]格式的日期，异常信息是:", e, str, format2);
            }
        }
        return null;
    }

    /**
     * 字符串转换日期
     * @param str
     * @return
     */
    public static Date stringToDate2(String str){
        //str =  " 2008-07-10 19:20:00 " 格式
        SimpleDateFormat format = new SimpleDateFormat(formatPattern_full);
        if(StringUtils.isNotEmpty(str)){
            try {
                return format.parse(str);
            } catch (ParseException e) {
                LogPortal.error("字符串[{}]转换[{}]格式的日期，异常信息是:",e,str,formatPattern_full);
            }
        }
        return null;
    }

    /**
     * 字符串转换日期
     * @param str
     * @return
     */
    public static Date str2DateByFormat(String str, String format){
        //str =  " 2008-07-10 19:20:00 " 格式
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        if(StringUtils.isNotEmpty(str)){
            try {
                return sdf.parse(str);
            } catch (ParseException e) {
                LogPortal.error("字符串[{}]转换[{}]格式的日期，异常信息是:",e,str,format);
            }
        }
        return null;
    }

    /**
     * 获取给定时间的哪一天的起始时间yyyy-MM-dd 00:00:00
     * @param date
     * @return
     */
    public static Date getDayStart(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    /**
     * 获取给定时间的哪一天的结束时间yyyy-MM-dd 23:59:59
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);

        return calendar.getTime();
    }

    public static Date longStringToDate(String time) {
        Date date = new Date();
        date.setTime(Long.parseLong(time));
        return date;
    }

    public static Date long2ToDate(Long time) {
        Date date = new Date();
        date.setTime(time);
        return date;
    }

    public static Date longStringToDateEnd(String time) {
        Calendar cl = Calendar.getInstance();
        cl.setTimeInMillis(Long.parseLong(time));

        cl.set(Calendar.HOUR_OF_DAY, 23);
        cl.set(Calendar.MINUTE, 59);
        cl.set(Calendar.SECOND, 59);

        return cl.getTime();
    }

    public static String long2FormatString(Long time){
        if(null == time){
            return "";
        }
        return sdf.format(new Date(time));
    }

    public static String long2FormatString(Object time){
        if(null == time){
            return "";
        }
        if("".equals(time.toString())){
            return "";
        }
        return sdf.format(new Date(Long.parseLong(time.toString())));
    }

    /**
     * 获取指定间隔前几年的起始时间
     * @param i
     * @return
     */
    public static Date getDayBeforeYear(int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.YEAR, -i);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    public static int getNowYear() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.YEAR);
    }

    public static int getNowDay() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.DAY_OF_MONTH);
    }
    public static int getNowMonth() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.MONTH)+1;
    }

    public static int getNowHour() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        return calendar.get(Calendar.HOUR_OF_DAY);
    }

    public static Date getDayBeforeMonth(int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.MONTH, -i);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    public static Date getDayBeforeDay(int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());

        calendar.add(Calendar.DAY_OF_YEAR, -i);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    public static Date getDayBeforeDay(Date currentDate,int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);

        calendar.add(Calendar.DAY_OF_YEAR, -i);

        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);

        return calendar.getTime();
    }

    public static Date getDayTimeBeforeDay(int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_YEAR, -i);
        return calendar.getTime();
    }

    public static Date getHourBeforeHour(Date currentDate,int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.HOUR_OF_DAY,-i);
        return calendar.getTime();
    }

    /**
     * @Author: shane
     * @Time: 2017/2/9 14:54
     * @Email: shanbaohua@lxfintech.com
     * @param: Date generateTime
     * @Return: Integer
     * @Throw:
     * @Desc: 将Date转换为年月日的Integer类型。e.g.2017-02-09 14:55:00 ==> 20170209
     */
    public static Integer dateToInteger(Date generateTime) {
        if (null == generateTime) {
            return null;
        }
        try {
            return Integer.parseInt(new SimpleDateFormat(formatPattern_Short).format(generateTime));
        } catch (Exception e) {
            LogPortal.error("dateToIntegerError, {}", e, generateTime);
            return null;
        }
    }

    public static Integer dateToInteger(Date generateTime, String format) {
        if (null == generateTime) {
            return null;
        }
        try {
            return Integer.parseInt(new SimpleDateFormat(format).format(generateTime));
        } catch (Exception e) {
            LogPortal.error("dateToIntegerError, {}", e, generateTime);
            return null;
        }
    }

    public static String dateToIntegerst(Date generateTime, String formatPattern) {
        if (null == generateTime) {
            return null;
        }
        try {
            return (new SimpleDateFormat(formatPattern).format(generateTime));
        } catch (Exception e) {
            LogPortal.error("dateToIntegerError, {}", e, generateTime);
            return null;
        }
    }

    /**
     * 将日期按照指定的格式转换为Long类型
     * @param generateTime
     * @param date2LongFormatEnum
     * @return
     */
    public static Long dateToLong(Date generateTime, Date2LongFormatEnum date2LongFormatEnum) {
        if (null == generateTime) {
            return null;
        }
        if(null == date2LongFormatEnum){
            date2LongFormatEnum = Date2LongFormatEnum.yyyyMMdd;
        }
        try {
            return Long.valueOf(new SimpleDateFormat(date2LongFormatEnum.name()).format(generateTime));
        } catch (Exception e) {
            LogPortal.error("dateToLongError, {}", e, generateTime);
            return null;
        }
    }

    public static List<Integer> getBetweenInteger(Date start, Date stop) {
        List<Integer> list = new ArrayList<>();

        Calendar begin = Calendar.getInstance();
        begin.setTime(start);
        Calendar end = Calendar.getInstance();
        end.setTime(stop);

        while (end.compareTo(begin) >= 0) {
            list.add(dateToInteger(begin.getTime()));
            begin.add(Calendar.DAY_OF_MONTH, 1);
        }
        return list;
    }

    /**
     * 获取当前日期
     * @return
     */
    public static Date getNowDate(){
        Date date = new Date();
        return date;
    }

    /**
     * 获取指定格式的日期字符串的明细
     * @param dateStr
     * @param formatPattern
     * @return
     */
    public static Calendar getDateStrCalendar(String dateStr,String formatPattern) {
        Date date = str2DateByFormat(dateStr,formatPattern);
        Calendar begin = Calendar.getInstance();
        begin.setTime(date);
        return begin;
    }


    /**
     * 获取当前日期的日日期字符串明细
     * @return
     */
    public static Calendar getNowDateStrCalendar() {
        Calendar begin = Calendar.getInstance();
        begin.setTime(getNowDate());
        return begin;
    }

    public static long getMillisAfterMillis2(Date date,int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, i);
        return calendar.getTimeInMillis();
    }

    public static long getMillisAfterMillis(Date date,int i) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MILLISECOND, i);
        return calendar.getTimeInMillis();
    }

    public static Date getUpDay(Date date,int dateIndex){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR,dateIndex);
        return calendar.getTime();
    }

    /**
     * 获取当前时间到D+1日0点零5分间隔毫秒数
     * @param currentDateIndex 格式:yyyyMMdd
     * @return
     */
    public static long getIntervalMilliSecond(int currentDateIndex) {
        Date currentDayDate = DateUtils.str2DateByFormat(currentDateIndex+"000000", DateUtils.formatPattern_full_01);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDayDate);
        calendar.add(Calendar.DAY_OF_YEAR,1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 5);
        calendar.set(Calendar.SECOND, 0);
        Date nextDayDate = calendar.getTime();

        Date currentDate = DateUtils.getNowDate();
        long expireTime = nextDayDate.getTime()-currentDate.getTime();
        return expireTime;
    }

    /**
     * 获取当前时间距离D+1日0点0分0秒的间隔毫秒数
     * @return
     */
    public static long getNextDayIntervalMilliSecond(Date currentDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(currentDate);
        calendar.add(Calendar.DAY_OF_YEAR,1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date nextDayDate = calendar.getTime();
        long expireTime = nextDayDate.getTime()-currentDate.getTime();
        if (expireTime<0){
            expireTime=0;
        }
        return expireTime;
    }

    public static void main(String[] args) {
        System.out.println(DateUtils.dateToString(DateUtils.getHourBeforeHour(DateUtils.getNowDate(),-2)));
    }
}
